package com.muki.spider.core;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;


/**
 * 通过消息队列获取 url
 * @author ljh_2017
 *
 */
@Component
public class UriGetor implements UriGet {

	private final Logger log = Logger.getLogger(UriGetor.class);
	private final RequestBuilder request = RequestBuilder.getInstance();
	private final UriRegisterationCore core = UriRegisterationCore.getInstance();
	
	@Override
	public void doGet(String topic,String url,int depth) {
		RequestUri r = request.getRequest();
		if(r!=null) {
			FlagUri uri = new FlagUri();
			uri.setTopic(topic);
			uri.setUri(url);
			uri.setDepth(depth);
			r.setFlagUri(uri);
			core.receive(r);
		} 
		request.releaseRequest(r);
	}
	
	@JmsListener(destination="urls")
	public void listener(Map<String,String> map) {
		String topic = map.get("topic");
		String url = map.get("link");
		int depth = Integer.parseInt(map.get("depth"))+1;
		if(SpiderUriMemoryCache.putIfAbsent(topic, url))
			doGet(topic,url,depth);
	}
}
